[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
struct Structure Data Type
A structure is an aggregate type. It allows multiple members to be
grouped together and named using a structure tag. Each member and may
have any data type, including other structures and unions, or be a
function. In C++, a struct is really a class, except that all members
are public by default (they are private by default in a class).
A special member is the bit field. A bit field is an object whose
size is measured in bits rather than bytes or words. Bit fields are
useful for mapping into hardware registers and memory locations, and
for packing binary flags into bytes or words without using the bit-
wise operators |, & and ^.
To define the layout of a structure, optionally give that layout a
tag name, and to define variables of that type, use the following
format:
struct [tag] {
member-1 declaration;
...
member-n declaration;
} [declarator [, declarator ... ]] ;
The format of a bit field declaration is:
type [identifier] : field-width;
Once a structure layout has been defined, it may be referenced in
subsequent declarations using the format:
struct tag declarator [, declarator ...] ;
In either case, the declarator may contain an initializer list.
When members are referenced, they must be qualified by their parent
structure name as in:
struct_name1.member_name1 = value1;
struct_name2.member_name2 = value2;
and since structures may be nested, the general naming format
becomes:
level-1.level-2. ... .member
Notes: If a structure layout has no tag, it cannot be referenced
in subsequent (and separate) declarations, casts, or
function definitions.
Structures may be nested and may contain members of any
type. A structure may not contain an instance of itself;
it may, however, contain a pointer to a structure of the
same type, something commonly done in defining linked-list
nodes.
Structures with different tag names define different
types, even if their layouts are identical. That is, such
structures are not assignment-compatible and should not
be copied to or compared with each other. If you really
mean two structure types to be the same, make them the
same. This also applies to structures without tags.
Each structure has its own identifier namespace; a struct
tag must be distinct from other enum, struct, and union
tags.
A structure may be assigned to using the assignment
operator =. A structure may be passed by value to
functions, and returned by value from functions.
A structure of class auto may not have an initializer
list.
-------------------------------- Example ---------------------------------
struct date {
int year;
int month;
int day;
} todays_date = {91, 5, 1};
struct date birth_date, *adate, dates[100];
date = (struct date) malloc(10 * sizeof(struct date));
/* Asynch communications adapter line control register */
struct lcreg {
unsigned int word_length : 2;
unsigned int stop_bits : 1;
unsigned int parity_enable : 1;
unsigned int even_parity : 1;
unsigned int stick_parity : 1;
unsigned int set_break : 1;
unsigned int divisor_latch : 1;
};
See Also:
class
Types
union
enum
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson